package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.domain.Movie;
import com.woniuxy.dto.JsonResultDto;
import com.woniuxy.entity.MovieElasticDemo;
import com.woniuxy.mapper.MovieMapper;
import com.woniuxy.param.MovieParam;
import com.woniuxy.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-26
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {
    @Autowired
    private ElasticsearchRestTemplate template;
    @Override
    public List<Movie> selectMovie(MovieParam movieParam) {
        if(movieParam==null){
            return this.list();
        }else {
            QueryWrapper<Movie> wrapper = new QueryWrapper<>();
            if (movieParam.getMIntroduction() != null)
                wrapper.eq("m_introduction", movieParam.getMIntroduction());
            if (movieParam.getMName() != null)
                wrapper.eq("m_name", movieParam.getMName());
            if (movieParam.getMStatus() != null)
                wrapper.eq("m_status", movieParam.getMStatus());
            return this.list(wrapper);
        }
    }

    @Override
    public JsonResultDto putAway() {
        List<Movie> movies = this.list();
        for(Movie movie:movies){
            if(movie.getmStatus().equals("1")){
                template.save(new MovieElasticDemo(
                        String.valueOf(movie.getmId()),
                        movie.getmName(),
                        movie.getmIntroduction(),
                        movie.getmStatus(),
                        movie.getmStarttime()
                ));
            }
        }
        return new JsonResultDto("00000","success",null,null);
    }
}
